<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>权限分配</title>
    <#include "../common/link.ftl">
    <script>
        $(function () {
            //全选/全不选
            $("#allCb").click(function () {
                //获取当前复选框checked状态,设置到table中的所有复选框
                $(".cb").prop('checked',$(this).prop('checked'))
            })

            //table中的复选框
            $(".cb").click(function () {
                //获取table中已经勾选的复选框的数量 , 判断是否等于table中的所有复选框的数量
                $("#allCb").prop('checked',$(".cb:checked").length == $(".cb").length)
            })

            $.each($(".cb"),function (index, ele) {
                var id = $(ele).data('id');
                console.log(id+","+$.inArray(id,${mypermissions}))
                if($.inArray(id,${mypermissions}) > -1){
                    $(this).prop("checked",true)
                }
            })


            // 保存权限设置
            $("#btn_submit").click(function () {
                var rid = $("#roleid").val();
                var cbs = $(".cb:checked");
                var ids = [];
                $.each(cbs,function (index, ele) {
                    ids.push($(ele).data('id'));
                })
                var all = $(".cb");
                var allcb = [];
                $.each(all,function (index, ele) {
                    allcb.push($(ele).data('id'));
                })
                $.post("/role/distributePermissions.do", {rid: rid, ids: ids, allcb: allcb}, function (data) {
                    console.log(data);
                    if (data.code == 200) {
                        location.href = "/role/list.do";
                    } else {
                        alert(data.msg);
                    }
                })
            })

            // 重置
            $("#btn_reset").click(function () {
                $("#allCb").prop('checked',false)
                $(".cb").prop('checked',false)
                var rid = $("#roleid").val();
                $.post("/role/deletePermissions.do", {rid:rid}, function (data) {
                    console.log("加载成功！");
                    if (data.code == 200) {
                        window.location.href = '/role/list.do';
                    } else {
                        alert(data.msg);
                    }
                })
            })

            // 取消
            $("#btn_default").click(function () {
                //document.referrer 上一个请求路径
                var url = "role/list.do";
                window.location.href = url;
            })
        })
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="role"/>
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>分配权限</h1>
        </section>
        <section class="content">
            <div class="box" >
                <div style="margin: 10px;">
                <!--高级查询--->
                <form class="form-inline" id="searchForm" action="/role/input.do?id=${role.id}" method="post">
                    <input type="hidden" name="currentPage" id="currentPage" value="1">
                    <input type="hidden" name="id" value="${(role.id)!}" id="roleid">
                    <input type="hidden" name="mypermissions" value="${(mypermissions)!}" id="mypermissions">
                    <div class="form-group">
                        <label for="keyword">关键字:</label>
                        <input type="text" class="form-control" id="keyword" name="keyword" value="${qo.keyword!}" placeholder="请输入权限名称/路径">
                    </div>
                    <button id="btn_query" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> 查询</button>
                    <button id="btn_submit"  type="button" class="btn btn-primary">分配权限</button>
                    <button id="btn_reset" type="button" class="btn btn-danger">重置权限</button>
                    <button id="btn_default" type="button" class="btn btn-default">取消</button>
                </form>
                </div>

                <table class="table table-striped table-hover">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="allCb"></th>
                        <th>编号</th>
                        <th>权限名称</th>
                        <th>权限路径</th>
                    </tr>
                    </thead>
                    <#list page.list as permission>
                       <tr>
                           <td><input type="checkbox" class="cb" data-id="${permission.id}"></td>
                           <td>${permission_index+1}</td>
                           <td>${permission.name}</td>
                           <td>${permission.expression!}</td>
                       </tr>
                    </#list>
                </table>
                <!--分页-->
                <#include "../common/page.ftl">
            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>
</body>
</html>
